<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Young
 */
 
interface User {
    //put your code here
    public function getName();
    public function setName($_name);
    public function setPassword($_password);
    public function getGroups();
    public function addGroup($_group);
    public function getFriends($_group);
    public function getFriendBooks($_friend);
    public function getFriendNotesOnBooks($_name,$_book);
   // public function getFriendNotesTopOnBooks($_name,$_book);
    //public function getFriendNotesDownOnBooks($_name,$_book);
}

class NormalUser implements User{
    private $name = "";
    protected $password = "";
    protected $books;
    protected $groups = array();
    public function __construct($_name) {
        $this->setName($_name);
        $con = mysql_connect("localhost", "root", "12345");
		mysql_query("set names utf8");
        if(!$con){
            die('Could not connect:  '.mysql_error());
        }
        mysql_select_db("mynotes",$con);
    }
	
	public static function getAllNormalUser()
	{
		$DB = mysql_connect("localhost","root","12345");
		mysql_query("set names utf8");
		mysql_select_db("mynotes",$DB);
		$DBT = mysql_query("SELECT * FROM user WHERE user_type = 'Normal'");
		$results = array();
		for ($i=0; $i< mysql_num_rows($DBT); $i++)
		{			
		  $row = mysql_fetch_array($DBT);
		  $us = new NormalUser($row['user_name']);
		  $results[] = $us;
		}  	
		return $results;
	}
	
	public static function deleteUser($delname)
	{
		$DB = mysql_connect("localhost","root","12345");
		mysql_query("set names utf8");
		mysql_select_db("mynotes",$DB);
		
		mysql_query("delete from user where user_name = '$delname'");
		mysql_query("delete from comments where commentUser = '$delname'");
		mysql_query("delete from notes where notesUser = '$delname'");
		mysql_query("delete from groups where username = '$delname'");
	}
	
	
    public function getName() {
        return $this->name;
    }
    public function setName($_name) {
        $this->name = $_name;
    }
    public function setPassword($_password) {
        $this->password = $_password;
    }
    public function addGroup($_group){
        array_push($this->groups, $_group);
    }
    public function getGroups() {
        
        $group_name_query = "SELECT DISTINCT group_name  FROM groups WHERE username='".$this->name."'";
        $group_names = mysql_query($group_name_query);
        
        while($row_of_group_names = mysql_fetch_array($group_names)){
            array_push($this->groups, $row_of_group_names['group_name']);
        }
        return $this->groups;
    }
    public function getFriends($_group) {
        $friends = array();
        $group_friends_query = "SELECT DISTINCT username FROM groups WHERE group_name='".$_group."' AND username !='".$this->name."'";
        $group_friends = mysql_query($group_friends_query);
        while($row_of_group_friends = mysql_fetch_array($group_friends)){
            array_push($friends, $row_of_group_friends['username']);
        }
        return $friends;
    }
    public function getFriendBooks($_friend) {
        $booklist = array();
        $book_list_query = "SELECT DISTINCT bookname FROM notes WHERE notesUser ='".$_friend."'";
        $book_list = mysql_query($book_list_query);
        while($row_of_book_list = mysql_fetch_array($book_list)){
            array_push($booklist, $row_of_book_list['bookname']);
        }
        return $booklist;
    }
    
    public function getFriendNotesOnBooks($_name,$_book) {
        $notelist = array();
        $note_list_query = "SELECT DISTINCT content FROM notes WHERE bookname ='".$_book."' AND notesuser !='".$this->name."' AND notesuser = '".$_name."'";
        $note_list = mysql_query($note_list_query);
        while($row_of_note_list = mysql_fetch_array($note_list)){
            array_push($notelist, $row_of_note_list['content']);
        }
        return $notelist;
    }
    
    private function notIn($a,$groupsIn){
        foreach($groupsIn as $groupIn){
            if($a == $groupIn)
                return false;
        }
        return true;
    }
    
    public function getGroupsNotIn(){
        $groupsNotIn = array();
        $groupsIn = $this->getGroups();
        $query = "SELECT DISTINCT group_name from groups WHERE username != '".$this->name."'";
        $result = mysql_query($query);
        while($row_of_result = mysql_fetch_array($result)){
            if($this->notIn($row_of_result['group_name'],$groupsIn) == true){
                array_push($groupsNotIn, $row_of_result['group_name']);
            }
        }
        return $groupsNotIn;
    }
    
    public function isTheAdmintrator($_groupName){
        $query = "SELECT DISTINCT group_name from groups WHERE administrator ='".$this->name."'";
        $result = mysql_query($query);
        while($row_of_result = mysql_fetch_array($result)){
            if($_groupName == $row_of_result['group_name'])
                return true;
        }
        return false;
    }
}

class AdminUser extends NormalUser{
    protected $groupIncontrol = array();
    public function getGroupInControl(){
        return $this->groupIncontrol;
    }
}
?>